<?php
/***************************************************************************
 *
 *   copyright            : (C) 2005 Winds of Storm
 *
 *   $Id: whoson.php,v 1.15 2007/01/30 21:02:43 nathan Exp $
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

require("config.php");
if ($_REQUEST['op'] != 'lite') {
	$full_header			= 1;

	if ($_REQUEST['sid'] != '') {
		$header[]['HEADER_TEXT']	= "<meta http-equiv=refresh content='60; url=whoson.$phpEx?sid={$_REQUEST['sid']}'>\n";
	} else {
		$header[]['HEADER_TEXT']	= "<meta http-equiv=refresh content='60; url=whoson.$phpEx'>\n";
	}
}
$body['class']	= 'whoson';
require($libdir . "init." . $phpEx);
require($libdir . "chat_functs." . $phpEx);

$template = new Template($config['TPL_DIR']);
if ($_REQUEST['op'] == 'lite') {
	$template->set_filenames(array('body' => 'whoson_lite.tpl'));
} else {
	$template->set_filenames(array('body' => 'whoson_body.tpl'));
}

$isMod	= 0;
$room	= 0;
if ($_REQUEST['sid'] != '') {
	$sid	= $_REQUEST['sid'];
	$sql	= "SELECT *
			FROM
				{$dbprefix}sessions AS s,
				{$dbprefix}rooms AS r
			WHERE	
				s.session_id='$sid' AND
				s.room_id=r.room_id";
	print("<!-- {$sql} -->\n");
	$result	= $db->sql_query($sql);
	$rows	= $db->sql_numrows($result);
	
	if ($rows != 0) {
		$row	= $db->sql_fetchrow($result);
		$room	= $row['room_id'];
		$isMod	= $row['moderator'];
	}
}

$users		= 0;
$roomgroups	= array();
$rooms		= array();
$userlist	= array();

$sql		= "SELECT * FROM {$dbprefix}room_groups";
$result		= $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result)) {
	$roomgroups[]	= $row;
}
$sql		= "SELECT * FROM {$dbprefix}rooms ";
if (!$isMod) {
	$sql	.= " WHERE room_dynamic != 'yes'";
}
$sql		.= " ORDER BY room_group,room_name";
$result		= $db->sql_query($sql);

while ($row = $db->sql_fetchrow($result)) {
	$rooms[]	= $row;
}

$sql	= "SELECT *
		FROM
			{$dbprefix}sessions AS s,
			{$dbprefix}prefs AS p
		WHERE
			room_id!=-1 AND
			s.user_id=p.user_id
		ORDER BY
			username";
$result	= $db->sql_query($sql);

while ($row = $db->sql_fetchrow($result)) {
	$userlist[]	= $row;
	$users++;
}

if ($users == 1) {
	$perpeople	= 'Person';
} else {
	$perpeople	= 'People';
}

$template->assign_vars(array(
	'USERS'		=> $users,
	'PERPEOPLE'	=> $perpeople,
	'U_PROFILE'	=> $profile_url
));

foreach ($roomgroups as $thisgroup) {
	$template->assign_block_vars('groups', array(
		'GROUP_NAME'	=> $thisgroup['group_name']
	));

	foreach ($rooms as $thisroom) {
		$room_ok	= 1;
		if ($_REQUEST['op'] == 'lite' && $_REQUEST['room_id'] != $thisroom['room_id']) {
			$room_ok	= 0;
		}
		if ($thisroom['room_group'] == $thisgroup['group_id'] && $room_ok) {
			$users	= 0;
			$topic	= '';
			$ulist	= '';
			$uarr	= '';
			foreach ($userlist as $thisuser) {
				if ($thisroom['room_id'] == $thisuser['room_id']) {
					$users++;
				}
			}
			if ($thisroom['room_topic']) {	
				$topic	= "-- " . $thisroom['room_topic'];
			}
			$template->assign_block_vars('groups.rooms', array(
				'ROOM_TOPIC'	=> $topic,
				'ROOM_NAME'	=> $thisroom['room_name'],
				'USERS'		=> $users
			));

			$ucount	= 0;
			foreach ($userlist as $thisuser) {
				if ($thisroom['room_id'] == $thisuser['room_id']) {
					$ucount++;
					$vars	= array();
					while (list($k, $v) = each($thisuser)) {
						$vars[strtoupper($k)] = $v;
					}

					if ($_REQUEST['op'] == 'lite') {
						if ($ucount != $users) {
							$vars['COMMA']	= ', ';
						}
					}
					if ($thisuser['idle_timer'] > 300) {
						$vars['IDLE']	= " <i>idle " . number_format($thisuser['idle_timer'] / 60, 0) . " minutes</i>";
					}
					if ($thisuser['moderator']) {
						$vars['ISMOD']	= " <i>(Moderator)</i>";
					}
					if ($thisuser['user_away'] == 1) {
						$vars['AWAY']	= "<i>Away message: " . $thisuser['user_away_msg'] . "</i><br />";
					}
					$vars['U_PROFILE']	= getProfileURL($thisuser);

					$template->assign_block_vars('groups.rooms.users', $vars);
				}
			}
		}
	}
}

$template->pparse('body');

